//定义js命名空间
Ext.namespace('FSBS.FsbsQmTrls');
//TODO : 完善数据结构
//DateTime -- date , decimal -- float
// { name: 'Department.Name', mapping: 'Department.Name' },
// { name: 'Department'},
//FsbsQmTrls FieldArray
FSBS.FsbsQmTrls.BaseFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'Trsfid', type: 'int' },
	{ name: 'CarId', type: 'int' },
	{ name: 'Exusid', type: 'string' },
	{ name: 'State', type: 'string' },
	{ name: 'IsDelete', type: 'bool' },
	{ name: 'Version', type: 'int' },
	{ name: 'CreateTime', type: 'DateTime' }
);

//FsbsQmTrls JsonReader
FSBS.FsbsQmTrls.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
    constructor: function (cfg) {
        cfg = cfg || {};
        FSBS.FsbsQmTrls.JsonReader.superclass.constructor.call(this, Ext.apply({
            fields: FSBS.FsbsQmTrls.BaseFieldsArray
        }, cfg));
    }
});

//FsbsQmTrls JsonStore
FSBS.FsbsQmTrls.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
	QryUrl: '/FsbsQmTrls/GetPlist', //查询Url
	UpdUrl: '/FsbsQmTrls/SaveOrUpdate', //更新记录Url
	DelUrl: '/FsbsQmTrls/Delete', //删除记录Url
    constructor: function (cfg) {
        cfg = cfg || {};
        FSBS.FsbsQmTrls.JsonStore.superclass.constructor.call(this, Ext.apply({
            fields : FSBS.FsbsQmTrls.BaseFieldsArray
        }, cfg));
    }
});

//FsbsQmTrls GridPanel
FSBS.FsbsQmTrls.GridPanel = Ext.extend(Infrastructure.BaseGridPanel, {
	// User property
	//基本处理Url
    baseUrl: '/FsbsQmTrls/',
	//提交保存Url
	formSubmitUrl: '/FsbsQmTrls/SaveOrUpdate', 
	//提交保存提示信息
	formSubmitMsg: '确定要保存吗?',	
    //状态
    itemState: '',
	columnMask: '1',
	
	// Property
    store: new FSBS.FsbsQmTrls.JsonStore(),
    
	//constructor
    initComponent: function () {
		//prepare
		
		var itemGrid = this;
        var cmdAddEntity = this.onAddEntity;
		//set store params
		this.store.setBaseParam('state', this.itemState);
		
		// The filter.
		// { dataIndex: 'Department.Name', type: 'list', options: JsHelper.GetFilterListData(Infrastructure.Params.DeptStore.data.items, 'Name', 'Name') },
        var gridFilter = new Ext.ux.grid.GridFilters({
            encode: true,
			filters: [
				{ dataIndex: 'Id', type: 'string' },
				{ dataIndex: 'Trsfid', type: 'string' },
				{ dataIndex: 'CarId', type: 'string' },
				{ dataIndex: 'Exusid', type: 'string' },
				{ dataIndex: 'State', type: 'string' },
				{ dataIndex: 'IsDelete', type: 'string' },
				{ dataIndex: 'Version', type: 'string' },
				{ dataIndex: 'CreateTime', type: 'string' }
            ]
        });
		
		// selModel
        var selModel = new Ext.grid.CheckboxSelectionModel({
            //header: '',
            //singleSelect: true,
            listeners: {
                'selectionchange': function (sm) {
                    // 控制工具栏按钮是否可用
                    itemGrid.delBtn.setDisabled(sm.getCount() < 1);
				}
			}
		});
		
		// columnModel 对于不常需要显示的 hidden: true 
		// { header: '出厂日期', dataIndex: 'ProdDate', xtype: 'datecolumn', format: 'Y-m-d' },
		//{ header: '状态', dataIndex: 'State',
		//			renderer: function (v) {
		//				return FsCarInfo.Params.CarStateStore.getById(v).get('Name');
		//			}
		//		}		
		var gridColumn = new Ext.grid.ColumnModel({
			columns: [
				new Ext.grid.RowNumberer(),
				selModel, 
				{ header: "调拨单ID", dataIndex: 'Trsfid' }, 
				{ header: "车辆ID", dataIndex: 'CarId' }, 
				{ header: "执行人ID", dataIndex: 'Exusid' }, 
				{ header: "状态", dataIndex: 'State' }
			],
			defaults: {
				sortable: true,
				editor: {
					xtype: 'textfield'
				}
			}
		});
		
		// PageBar
        var pagebBar = new Infrastructure.BasePagingToolbar({
            store: this.store,
            plugins: this.plugins
        });
		
		// 常用工具栏
        var toolBar = [{
            xtype: 'tbaraddbtn',
            menuid: '0111',
            handler: function () {
				itemGrid.onShowItemWin(true);
			}
        }, {
            menuid: '0111',
            xtype: 'tbarseparator'
        }, {
			xtype: 'tbardelbtn',
			menuid: '0112',
			handler: function () {
				itemGrid.delEntity();
			}
        }, {
            menuid: '0112',
            xtype: 'tbarseparator'
		}, {
			xtype: 'tbareditbtn',
			menuid: '0113',
			handler: function () {
				itemGrid.onShowItemWin(true, itemGrid.getSelectionModel().getSelected());
			}
		}, {
            menuid: '0113',
            xtype: 'tbarseparator'
		}, {
			xtype: 'tbarmenubtn',
			menuid: '0114',
			ref: '../actBtn',
			text: '提交',
			disabled: true,
			icon: '/Content/icons/accept.png',
			handler: function () {
				itemGrid.tbarSubmit(itemGrid.baseUrl + 'ActionMethod', '确定提交吗?', '提交已成功处理。');
			}
		}, {
            menuid: '0114',
            xtype: 'tbarseparator'
        }, {
            xtype: 'textfield',
            width: 150,
           id: 'q_query',
			ref: '../q_query',
			listeners: {
				specialkey: function (field, e) {
					if (e.getKey() == e.ENTER) {
						itemGrid.qryList(field.getValue());
					}
				}
			}
        }, {
            xtype: 'tbarqrybtn',
            handler: function (e) {
				itemGrid.qryList(itemGrid.q_query.getValue());
			},
            scope: itemGrid
        }, '-'];
		
		Ext.apply(this, {
            plugins: [gridFilter],
            sm: selModel,
            cm: gridColumn,
            tbar: toolBar,
            bbar: pagebBar,
            listeners: {
                'rowdblclick': function (grid, rowIndex, e) {
					this.onShowItemWin(true, grid.store.getAt(rowIndex));
				}
            }
        });
		
        FSBS.FsbsQmTrls.GridPanel.superclass.initComponent.call(this);
    },
	
	/****************** 
    * user function 
    ******************/
	// 读取信息显示
	onShowItemWin: function (isEdit, selRecord) {
		var itemGrid = this;
		var dataId = Ext.isDefined(selRecord) ? selRecord.data.Id : '';
		var itemEditForm = new FSBS.FsbsQmTrls.EditForm({
			isEdit: isEdit,
			submitUrl: this.formSubmitUrl,
			dataId: dataId,
			dataRecord: selRecord,
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						if (!Ext.isDefined(selRecord))
							ele.form.reset();
						itemGrid.store.reload();
						JsHelper.ShowInfo('信息更新成功!');
					}
				}
			}
		});
		
		var itemEditWin = new Infrastructure.BaseWindow({
			title: 'FSBS.FsbsQmTrls',
			//iconCls: 'car',
			width: 600,
			items: [itemEditForm],
			buttons: [new Ext.ux.SubmitBtn({
				menuid: '',
				hidden: !(isEdit && itemEditForm.submitUrl != ''),
				handler: function () {
					itemEditForm.saveItem(itemEditForm.submitUrl)
				},
				scope: itemEditForm
			}), new Ext.ux.CancelBtn({
				handler: function () {
					itemEditForm.ownerCt.destroy();
				}
			})]
		});
		itemEditWin.show();
		if (Ext.isDefined(selRecord)) {
			itemEditForm.form.loadRecord(selRecord);
		}
	}
});

//FsbsQmTrls EditForm
FSBS.FsbsQmTrls.EditForm = Ext.extend(Infrastructure.BaseForm, {
	dataId : null,
	dataRecord: null,
	submitUrl: '/FsbsQmTrls/SaveOrUpdate',
	submitMsg: '确定要保存吗?',
		
    initComponent: function () {
		var itemEditForm = this;
		
        //Form第一列
        // 定义表单列
		var firstColumn = [{
			fieldLabel: "调拨单ID",
			maxLength: 0,
			name: 'Trsfid'
		}, {
			fieldLabel: "车辆ID",
			maxLength: 0,
			name: 'CarId'
		}, {
			fieldLabel: "执行人ID",
			maxLength: 36,
			name: 'Exusid'
		}, {
			fieldLabel: "状态",
			maxLength: 10,
			name: 'State'
		}, ];

        //Form第二列
        var secondColumn = [{
            fieldLabel: 'Column2',
			readOnly: true
        }];

        //Form单列
        var singleColumn = [{
            xtype: 'textarea',
            fieldLabel: '描述',
            name: 'Description',
            height: 50,
            width: 430
        }];

        Ext.apply(this, {
            items: [{
                layout: 'column',
                // 注意 现在使用的是两列 要是三列或更多
                // 请改变下面的items
                // 并在上面定义每个列的组件集合
                items: [{
                    columnWidth: .5,
                    bodyStyle: 'padding:1px',
                    layout: 'form',
                    defaultType: 'textfield',
					defaults: {
						width: 150,
						allowBlank: false,
						readOnly: !this.isEdit
					},
                    items: firstColumn
                }, {
                    columnWidth: .5,
                    bodyStyle: 'padding:1px',
                    layout: 'form',
                    defaultType: 'textfield',
					defaults: {
						width: 150,
						allowBlank: false,
						readOnly: !this.isEdit
					},
                    items: secondColumn
                }]
            }, singleColumn],
            reader: new FSBS.FsbsQmTrls.JsonReader()
        });
		
		FSBS.FsbsQmTrls.EditForm.superclass.initComponent.call(this);
		
		//定义保存后执行的事件
        this.addEvents('afterSave');
    }
});

//FsbsQmTrls DetailWin
FSBS.FsbsQmTrls.EditWin = Ext.extend(Ext.Window, {
	title: 'FsbsQmTrls Infomation',
    iconCls: 'application_form',
    width: 600,
    resizable: false,
    constrain: true,
    autoHeight: true,
    modal: true,
    closeAction: 'close',
    plain: true,
	buttonAlign: 'center',
	
	//constructor
    initComponent: function () {
        FSBS.FsbsQmTrls.EditWin.superclass.initComponent.call(this);
	}
});
